package com.jiakai.cashinterfacemucon.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiakai.cashinterfacemucon.model.Product;
import com.jiakai.cashinterfacemucon.model.ProductID;
import com.jiakai.cashinterfacemucon.service.ProductIDService;
import com.jiakai.cashinterfacemucon.service.ProductService;
import com.lishunyi.result.VO.ResultVO;
import com.lishunyi.result.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/cashinterface-mucon")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductIDService idService;

    /**
     * 获取所有产品productId与parkID
     * 每次获取之前会删除该表
     * 搜索之后重新写入
     * @return 所有的productId跟parkID的关系
     */
    @RequestMapping("/ids/get")
    public ResultVO getIds(){
        idService.deleteAll();
        List<Product> products =  productService.findAllProductId();
        List<ProductID> ids = new ArrayList<>();
        for (Product p : products){
            ProductID pid = new ProductID();
            pid.setProductId(p.getProductId());
            pid.setParkID(p.getParkID());
            ids.add(pid);
        }
        int i = 0;
        for (ProductID id : ids){
            i += idService.insert(id);
        }
        return ResultUtils.success(ids);
    }

    /**
     * 获取所有未绑定的产品信息
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return pageinfo
     */
    @RequestMapping("/product/get")
    public ResultVO findAllUnbind(
            @RequestParam(required = false,defaultValue = "1",name = "pageNum")Integer pageNum,
            @RequestParam(required = false,defaultValue = "10",name = "pageSize")Integer pageSize
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productService.findAllUnbind();
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        return ResultUtils.success(pageInfo);
    }

}
